#ifndef SERVICE_URL_H
#define SERVICE_URL_H
#include <string>



#include <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
namespace vpm
{
	namespace mina
	{
		//represent a service address, like:
		// tcp://192.168.0.111:1900 
		// when used for client, the client will connect to 192.168.0.111:1900
		//see the definition of the url http://www.ietf.org/rfc/rfc1738.txt
		//not finished 

		class ServiceUrl
		{
		public:
			enum SchemeType
			{
				NONE,
				TCP,
				UDP,
				//...
			};
			
			~ServiceUrl()
			{

			}

			ServiceUrl(const std::string & url)
			{				
				parse_url(url); 
			}    
			bool parse_url(const std::string & url )
			{
				//tcp://192.168.0.1:9999
				char  szScheme[32]; 
				char  szHost[256];
				
				size_t pos =0; 

				const char *pCh =  url.c_str(); 
				//get scheme : tcp/udp
				while( pos <sizeof(szScheme))
				{
					if (*pCh !=0 )
					{
						szScheme[pos++] = (*pCh == ':')? 0:*pCh; 					
						if (*pCh == ':')
						{				
							pCh++; 
							break; 
						}
						pCh++; 
					}
					else 
					{
						szScheme[pos] =0; 
						return false; 
					}
				}
				 
				//skip the two // 
				pos =0; 
				while (*pCh!= 0 && pos < 2 )
				{
					pos ++; 
					if (*pCh != '/')
					{
						return false; 
					}
					pCh ++; 
				}

				//get host part
				pos =0; 
				while (pos < sizeof(szHost))
				{
					if ( *pCh !=0 )
					{
						szHost[pos++] = (*pCh == ':')? 0:*pCh; 		
						if (*pCh ==':')
						{		
							pCh++; 
							break; 
						}		 
						pCh ++; 
					}
					else 
					{
						szHost[pos] = 0; 
						return false; 
					}

				}
			 
				//get port 
				if (pCh	)
				{			
					port = atoi(pCh); 
				}
				host = szHost; 
				scheme = szScheme; 
				return true;
			}

			void to_string()
			{
				std::cout <<  scheme << " "<<schemeType << " "  << host << " " << port << " \n"; 
			}
 
			SchemeType schemeType;
			std::string scheme; 			
			std::string host; 
			unsigned int port;
			std::string urlPath; //url path should be different from schemes, so it should be parsed in other way 

		
		private:
			std::string m_url; 
		};
	}
}


#endif // SERVICE_URL_H


